package com.hertz.appr.pojo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.hertz.appr.http.MyHttpClient;
import com.hertz.appr.http.MyHttpElement;
import com.hertz.appr.http.Parameter;
import com.hertz.appr.http.PostParameter;
import com.hertz.appr.http.Response;
import com.hertz.appr.json.JSONArray;
import com.hertz.appr.json.JSONException;
import com.hertz.appr.json.JSONObject;
import com.hertz.appr.oauth.AccessToken;
import com.hertz.appr.oauth.OAuth;
import com.hertz.appr.pojo.db.Message;
import com.hertz.appr.pojo.db.Robot;
import com.hertz.appr.service.RobotGang;
import com.hertz.appr.service.RobotGangTestImpl;
import com.hertz.appr.util.Configuration;
import com.hertz.appr.util.WeiboException;

public class WeiBo {

	private Logger log = Logger.getLogger(WeiBo.class);
	private String provider;
	private MyHttpClient httpClient;
	
	private String content;
	private String toUid;
	private String toNick;
	private String sourceId;
	private Short type;
	
	private String add_pic_api;
	private String add_api;
	private String comment_api;
	private RobotGang robotGang;
	private String readd_api;
	
	
	public WeiBo(String provider){
		this.provider = provider;
	}
	
	public void init(){
		add_pic_api = Configuration.getProperty(provider + ".weibo.api.t.add_pic");
		add_api = Configuration.getProperty(provider + ".weibo.api.t.add");
		comment_api = Configuration.getProperty(provider + ".weibo.api.t.comment");
		readd_api = Configuration.getProperty(provider + ".weibo.api.t.readd");
		this.httpClient = new MyHttpClient(provider);
	}
	
	public boolean sendReadd(String msg,String reid){
		boolean result = false;
		List<Parameter> parms = new ArrayList<Parameter>();
		if(provider.equals(Configuration.QQ_PROVIDER)){
			parms.add(new Parameter("content", msg));
			parms.add(new Parameter("clientip", "127.0.0.1"));
			parms.add(new Parameter("reid",reid));
		}else if(provider.equals(Configuration.SINA_PROVIDER)){
			log.debug("sina api for comment");
		}
		OAuth oauth = robotGang.getOauth(provider, Robot.type_rw,Robot.type_w);
		if(oauth == null){
			log.info("no oauth,may be api limit");
			return false;
		}
		MyHttpElement he = oauth.getOauthHttpElement(readd_api,MyHttpClient.POST, parms);
		log.debug("queryString : " + he.getQueryString());
    	try {
			Response response = httpClient.httpRequest(readd_api, he);
			log.debug("comment result = " + response.toString());
			result = isSendMsgSuccess(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean sendComment(String msg,String reid){
		boolean result = false;
		List<Parameter> parms = new ArrayList<Parameter>();
		if(provider.equals(Configuration.QQ_PROVIDER)){
			parms.add(new Parameter("content", msg));
			parms.add(new Parameter("clientip", "127.0.0.1"));
			parms.add(new Parameter("reid",reid));
		}else if(provider.equals(Configuration.SINA_PROVIDER)){
			log.debug("sina api for comment");
		}
		OAuth oauth = robotGang.getOauth(provider, Robot.type_rw,Robot.type_w);
		if(oauth == null){
			log.info("no oauth,may be api limit");
			return false;
		}
		MyHttpElement he = oauth.getOauthHttpElement(comment_api,MyHttpClient.POST, parms);
		log.debug("queryString : " + he.getQueryString());
    	try {
			Response response = httpClient.httpRequest(comment_api, he);
			log.debug("comment result = " + response.toString());
			result = isSendMsgSuccess(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean sendMsg(String msg){
		boolean result = false;
		OAuth oauth = robotGang.getOauth(provider, Robot.type_rw,Robot.type_w);
		if(oauth == null){
			log.info("no oauth,may be api limit");
			return false;
		}
		List<Parameter> parms = new ArrayList<Parameter>();
		
		if(provider.equals(Configuration.QQ_PROVIDER)){
			parms.add(new Parameter("content", msg));
			parms.add(new Parameter("clientip", "127.0.0.1"));
		}else if(provider.equals(Configuration.SINA_PROVIDER)){
			parms.add(new Parameter("status", msg));
		}
		
		MyHttpElement he = oauth.getOauthHttpElement(add_api,MyHttpClient.POST, parms);
		log.debug("queryString : " + he.getQueryString());
    	try {
			Response response = httpClient.httpRequest(add_api, he);
			log.debug("send message result = " + response.toString());
			result = isSendMsgSuccess(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public boolean sendMsgWithImg(String msg,File file){
		boolean result = false;
		List<Parameter> parms = new ArrayList<Parameter>();
		
		if(provider.equals(Configuration.QQ_PROVIDER)){
			parms.add(new Parameter("content", msg));
			parms.add(new Parameter("clientip", "127.0.0.1"));
		}else if(provider.equals(Configuration.SINA_PROVIDER)){
			parms.add(new Parameter("status", msg));
//			parms.add(new Parameter(name, value))
		}
		OAuth oauth = robotGang.getOauth(provider, Robot.type_rw,Robot.type_w);
		if(oauth == null){
			log.info("no oauth,may be api limit");
			return false;
		}
		MyHttpElement he = oauth.getOauthHttpElement(add_pic_api,MyHttpClient.POST, parms);
		log.debug("queryString : " + he.getQueryString());
    	try {
			Response response = httpClient.httpPostPic(add_pic_api, he.getParams(), file, he.getHeaderParams());
			log.debug("send message result = " + response.toString());
			result = isSendMsgSuccess(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private boolean isSendMsgSuccess(Response res){
		boolean result = false;
		if(res == null){
			return result;
		}
		try {
			JSONArray ja = null;
			JSONObject jsonObject = res.asJSONObject();
			if(provider.equals(Configuration.QQ_PROVIDER)){
				
				if(jsonObject.getString("ret") == null || !jsonObject.getString("ret").equals("0")){
					log.debug("result is error,response=" + res);
				}else{
					result = true;
				}
			}else{
				if(StringUtils.isBlank(jsonObject.getString("id"))){
					result = false;
				}else 
					result = true;
			}
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        } 
        return result;
	}
	
	private List<Parameter> buildCommentParams(){
		List<Parameter> queryParams = new ArrayList<Parameter>();
		if(provider.equals(Configuration.QQ_PROVIDER)){
			queryParams.add(new Parameter("format", "json"));
			queryParams.add(new Parameter("content", content));
			queryParams.add(new Parameter("clientip", "127.0.0.1"));
			queryParams.add(new Parameter("reid", sourceId));
			
		}else if(provider.equals(Configuration.SINA_PROVIDER)){
			queryParams.add(new Parameter("without_mention", "0"));
			queryParams.add(new Parameter("comment", content));
			queryParams.add(new Parameter("id", sourceId));
		}
		return queryParams;
	}
	
	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getToUid() {
		return toUid;
	}

	public void setToUid(String toUid) {
		this.toUid = toUid;
	}

	public String getToNick() {
		return toNick;
	}

	public void setToNick(String toNick) {
		this.toNick = toNick;
	}

	public String getSourceId() {
		return sourceId;
	}

	public void setSourceId(String sourceId) {
		this.sourceId = sourceId;
	}

	public Short getType() {
		return type;
	}

	public void setType(Short type) {
		this.type = type;
	}

	public RobotGang getRobotGang() {
		return robotGang;
	}

	public void setRobotGang(RobotGang robotGang) {
		this.robotGang = robotGang;
	}
}
